package com.bicon.botu.finance.payment.service;

import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.finance.payment.entity.UnionpayDto;
import com.bicon.botu.finance.payment.entity.AliPayDto;
import com.bicon.botu.finance.payment.vo.PayWayVO;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.finance.payment.entity.AppPaymentInfoEntity;
import com.bicon.botu.finance.payment.entity.WechatPayDto;
import com.bicon.botu.finance.payment.repository.AppPaymentInfoRepository;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by L on 2017/6/22.
 */
@Service
@Slf4j
public class AppPaymentInfoService {

    /**
     * 正式
     */
    String CHARSET = "utf-8";

    @Autowired
    private AppPaymentInfoRepository appPaymentInfoRepository;

    /**
     * 添加APP支付信息
     *
     * @param infoEntity app code
     */
    public BaseResult addPaymentInfo(AppPaymentInfoEntity infoEntity) {
        try {
            if (StringUtils.isEmpty(infoEntity.getCode()) || StringUtils.isEmpty(infoEntity.getNotifyUrl())) {
                return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
            }
            AppPaymentInfoEntity currentPayment = appPaymentInfoRepository.findOneByCode(infoEntity.getCode());
            if (currentPayment != null) {
                return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "code is used!");
            }

            if (infoEntity.getAliPayDto() != null) {
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayAppId())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的APP ID不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayAppPrivateKey())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的接口加密私钥不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayAppPublicKey())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的接口加密公钥不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayPublicKey())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台公钥不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayAccount())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的签约账号不能为空");
                }

                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getPayerAccount())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的支付(买家)账号不能为空");
                }

                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getPayerName())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的支付(买家)名称不能为空");
                }
            }

            if (infoEntity.getWechatPayDto() != null) {
                if (StringUtils.isEmpty(infoEntity.getWechatPayDto().getWechatAppId())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在微信平台的APP ID不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getWechatPayDto().getWechatAppSecret())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在微信平台的安全码不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getWechatPayDto().getWechatAppPartner())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在微信平台的商户ID不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getWechatPayDto().getWechatAppKey())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在微信平台的key不能为空");
                }
            }
            if (infoEntity.getUnionpayDto() != null) {
                BaseResult x = verifyUnionPay(infoEntity);
                if (x != null) {
                    return x;
                }
            }
            if (infoEntity.getAliPayDto() == null && infoEntity.getWechatPayDto() == null && infoEntity.getUnionpayDto() == null) {
                return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
            }
            infoEntity.preInsert();
            AppPaymentInfoEntity resultInfoEntity = appPaymentInfoRepository.save(infoEntity);
            if (resultInfoEntity != null) {
                return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, resultInfoEntity);
            } else {
                return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500 );
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("error", e);
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500 );
        }
    }

    private BaseResult verifyUnionPay(AppPaymentInfoEntity infoEntity) {
        if (StringUtils.isEmpty(infoEntity.getUnionpayDto().getUnionAppId())) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在银联平台的APP ID不能为空");
        }
        if (StringUtils.isEmpty(infoEntity.getUnionpayDto().getUnionMerId())) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在银联平台的商户 ID不能为空");
        }
        if (StringUtils.isEmpty(infoEntity.getUnionpayDto().getSignCertPath())) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在银联平台的签名证书名称不能为空");
        }
        if (StringUtils.isEmpty(infoEntity.getUnionpayDto().getSignCertPwd())) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在银联平台的签名证书密码不能为空");
        }
        if (StringUtils.isEmpty(infoEntity.getUnionpayDto().getEncryptCert())) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在银联平台的敏感信息加密证书不能为空");
        }
        if (StringUtils.isEmpty(infoEntity.getUnionpayDto().getMiddleCert())) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在银联平台的验签中级证书不能为空");
        }
        if (StringUtils.isEmpty(infoEntity.getUnionpayDto().getRootCert())) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在银联平台的验签根证书不能为空");
        }
        return null;
    }


    /**
     * 更新APP支付信息
     *
     * @param infoEntity app code
     */
    public BaseResult updatePaymentInfo(AppPaymentInfoEntity infoEntity) {
        try {
            if (StringUtils.isEmpty(infoEntity.getCode()) || StringUtils.isEmpty(infoEntity.getId())) {
                return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
            }
            AppPaymentInfoEntity currentPayment = appPaymentInfoRepository.findById(infoEntity.getId()).orElse(null);;
            if (currentPayment == null || !currentPayment.getCode().equals(infoEntity.getCode())) {
                return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
            }
            if (infoEntity.getAliPayDto() != null) {
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayAppId())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的APP ID不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayAppPrivateKey())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的接口加密私钥不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayAppPublicKey())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的接口加密公钥不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayPublicKey())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台公钥不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getAliPayDto().getAlipayAccount())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在支付宝平台的签约账号不能为空");
                }
            }

            if (infoEntity.getWechatPayDto() != null) {
                if (StringUtils.isEmpty(infoEntity.getWechatPayDto().getWechatAppId())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在微信平台的APP ID不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getWechatPayDto().getWechatAppSecret())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在微信平台的安全码不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getWechatPayDto().getWechatAppPartner())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在微信平台的商户ID不能为空");
                }
                if (StringUtils.isEmpty(infoEntity.getWechatPayDto().getWechatAppKey())) {
                    return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "APP在微信平台的参数加密key不能为空");
                }

            }
            if (infoEntity.getUnionpayDto() != null) {
                BaseResult x = verifyUnionPay(infoEntity);
                if (x != null) {
                    return x;
                }
            }
            if (infoEntity.getAliPayDto() == null && infoEntity.getWechatPayDto() == null) {
                return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
            }
            AppPaymentInfoEntity resultInfoEntity = appPaymentInfoRepository.save(infoEntity);
            if (resultInfoEntity != null) {
                return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, resultInfoEntity);
            } else {
                return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500 );
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("error", e);
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500 );
        }
    }


    /**
     * 列出该APP支持的支付方式
     *
     * @param code
     * @return
     */
    public BaseResult listPay(String code) {
        try {
            AppPaymentInfoEntity info = appPaymentInfoRepository.findOneByCode(code);
            if (info == null) {
                return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
            }
            List<PayWayVO> list = new ArrayList<>();
            if (info.getAliPayDto() != null) {
                list.add(new PayWayVO(AliPayDto.NAME_ALIPAY, AliPayDto.TYPE_ALIPAY));
            }
            if (info.getWechatPayDto() != null) {
                list.add(new PayWayVO(WechatPayDto.NAME_WECHAT, WechatPayDto.TYPE_WECHAT));
            }
            if (info.getUnionpayDto() != null) {
                list.add(new PayWayVO(UnionpayDto.NAME_UNION, UnionpayDto.TYPE_UNION));
            }
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, list);
        } catch (Exception e) {
            log.info("error", e);
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500 );
        }

    }


    /**
     * 查找第三方信息
     *
     * @param code
     * @return
     */
    public AppPaymentInfoEntity findOneByCode(String code) {
        return appPaymentInfoRepository.findOneByCode(code);
    }
}
